package com.ciwong.mobilelib.utils;

import android.content.Context;

import com.ciwong.libs.utils.CWLog;
import com.ciwong.libs.utils.CWSys;
import com.ciwong.libs.utils.volley.BaseRequest;

/**
 * 习网统计工具类
 *
 * @author bin
 * @version ciwong v.1.0 2015/10/22 10:31
 * @since ciwong v.1.0
 */
public class StatUtils {
    private static final String TAG = StatUtils.class.getSimpleName();

    /**
     * 新会话时长
     */
    private static final long NEW_SESSION_TIME = 3000;

    /**
     * 用户ID
     */
    public static long userId;
    public static String userName;

    /**
     * 学校ID
     */
    public static int schoolId;

    /**
     * 品牌ID
     */
    public static int brand;

    /**
     * 应用更新URL
     */
    public static String HOST = "http://eapi.ciwong.com/repos/570678344/android/update";

    public static synchronized void onResume(Context context) {

        String lastExitPosition = CWSys.getSharedString(
                BaseConstants.SHARE_KEY_LAST_EXIT_POSITION, "");
        if (lastExitPosition.equalsIgnoreCase(context.getClass().getName())) {
            long lastExitTime = CWSys
                    .getSharedLong(BaseConstants.SHARE_KEY_LAST_EXIT_TIME, 0l);
            if (lastExitTime + NEW_SESSION_TIME < System.currentTimeMillis()) {
                CWLog.e(TAG, "new session!");
//                statVisits(Operation.OPERATION_TYPE_VISITS);
            }
        }
    }

    public static synchronized void onPause(Context context) {
        try {
            CWSys.setSharedString(BaseConstants.SHARE_KEY_LAST_EXIT_POSITION,
                    context.getClass().getName());
            CWSys.setSharedLong(BaseConstants.SHARE_KEY_LAST_EXIT_TIME,
                    System.currentTimeMillis());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 操作类型实体类
     */
    public class Operation {
        /**
         * 操作类型：访问量
         */
        public static final int OPERATION_TYPE_VISITS = 0;

        /**
         * 操作类型：扫码
         */
        public static final int OPERATION_TYPE_SCAN = 1;
    }

    /**
     * 习网统计网络请求类
     */
    public static class MAction implements BaseRequest.CWAction {
        /**
         * 全媒体电子报统计
         */
        public static final String ACTION_STAT_VISITS = HOST + "/v2/pay/Statistic/Visits";

        /**
         * APP上传下载统计
         */
        public static final String ACTION_DATA_STATISTICS = HOST + "/v1/epaperwork/send_data_flow";

        @Override
        public String getActionUrl(String s) {
            if (s.equals(ACTION_STAT_VISITS)) {
                return ACTION_STAT_VISITS;
            } else if (s.equals(ACTION_DATA_STATISTICS)) {
                return ACTION_DATA_STATISTICS;
            }
            return null;
        }
    }
}
